<?php
/**
 * 品牌管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;
class Brand extends Common
{
    function initialize()
    {
        parent::initialize();
    }
    public function index(){
        if(request()->isPost()){
            $where [] = ['del','=',0];
            $key = input('key');
            if($key){
                $where[] = ['title','like','%'.$key.'%'];
            }
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = db('goods_brand')
                ->where($where)
                ->order('sort','desc')
                ->order('id','desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return view();
    }
    public function add(){
        if(request()->isPost()){
            $data = Request::except('file');
            $data['create_time'] = date('Y-m-d H:i:s');
            $lastID = Db::name('goods_brand')->insertGetId($data);
            db('goods_category')->insert(
                [
                    'id'=> $lastID,
                    'title'=> $data['title'],
                    'title_en'=> $data['title_en'],
                    'pid'=>0,
                    'del'=>0,
                    'create_time'=> $data['create_time'],
                ]
            );
            db('news_category')->insert(
                [
                    'id' => $lastID,
                    'title' => $data['title'],
                    'title_en' => $data['title_en'],
                    'pid' => 0,
                    'del' => 0,
                    'create_time' => $data['create_time'],
                ]
            );
            return $result = ['code'=>1,'msg'=>'添加成功!','url'=>url('index')];
        }
        $this->assign('title','新增品牌');
        $this->assign('info','null');
        return $this->fetch('form');
    }
    public function order(){
        $db=db('goods_brand');
        $data = input('post.');
        if($db->update($data)!==false){
            return $result = ['code'=>1,'msg'=>'排序更新成功!','url'=>url('index')];
        }else{
            return $result = ['code'=>0,'msg'=>'排序更新失败!'];
        }
    }

    public function del(){
        $id = input('id');
        $res = db('goods_brand')->where('id',$id)->update(['del'=>1]);
        if($res){
            $result = ['code'=>1,'msg'=>'删除成功!'];
        }else{
            $result = ['code'=>0,'msg'=>'删除失败!'];
        }
        return $result;
    }
    public function delall(){
        $map[] =array('id','in',input('param.ids/a'));
        db('goods_brand')->where($map)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功!'];
    }

    public function edit(){
        if(request()->isPost()){
            $data = Request::except('file');
            $res = db('goods_brand')->where('id',$data['id'])->update($data);
            db('goods_category')->where('id', $data['id'])->update([
                'title'=> $data['title'],
                'title_en'=> $data['title_en'],
            ]);
            db('news_category')->where('id', $data['id'])->update([
                'title'=> $data['title'],
                'title_en'=> $data['title_en'],
            ]);
            if($res) {
                return json(['code' => 1, 'msg' => '保存成功!', 'url' => url('index')]);
            } else {
                return json(['code' => 0, 'msg' =>'保存失败！']);
            }
        }else{
            $id = input('id');
            $info = db('goods_brand')->where('id',$id)->find();
            $this->assign('title','编辑品牌');
            $this->assign('info',json_encode($info,true));
            return $this->fetch('form');
        }
    }

    //设置展示
    public function setOpen()
    {
        $id = input('post.id');
        $is_show = input('post.is_show');
        if (db('goods_brand')->where('id=' . $id)->update(['is_show' => $is_show]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }
}